<template>
  <el-dialog id="edit-dialog" :title="formValidate.id ? $t('appMarketing.recommend.add') : $t('appMarketing.recommend.edit')" :visible.sync="showState" :destroy-on-close="true" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item :label="$t('appMarketing.recommend.table.name')" prop="name">
        <el-input v-model="formValidate.name" clearable :placeholder="$t('appMarketing.recommend.prompt.name')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('appMarketing.recommend.table.recommendSort')" prop="sort">
        <el-input-number
          v-model="formValidate.sort"
          :controls="false"
          :max="1000"
          :placeholder="$t('appMarketing.recommend.prompt.recommendSort')"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item :label="$t('appMarketing.recommend.table.type')" prop="type">
        <el-select v-model="formValidate.type" :placeholder="$t('appMarketing.recommend.prompt.type')" style="width: 100%">
          <el-option
            v-for="item in $t('appMarketing.recommend.type')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
        <el-input v-if="formValidate.type === 'THIRD_PARTY_LINK'" v-model="formValidate.recommendUrl" clearable :placeholder="$t('appMarketing.recommend.prompt.recommendUrl')" :maxlength="500" style="margin-top: 10px" />
      </el-form-item>
      <el-form-item :label="$t('appMarketing.recommend.table.img')" prop="file">
        <up-load-image
          ref="upload"
          :img-width="150"
          :img-height="150"
          :img-url="formValidate.url"
          @getUrl="getImgUrl"
        />
        {{ $t('appMarketing.recommend.recommendedSize') }} 660px * 230px
      </el-form-item>
      <el-form-item :label="$t('appMarketing.recommend.table.state')" prop="state">
        <el-select v-model="formValidate.state" :placeholder="$t('appMarketing.recommend.prompt.state')" style="width: 100%">
          <el-option
            v-for="item in $t('appMarketing.recommend.state')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateRecommend, addRecommend } from '@/api/operation'
import UpLoadImage from '@/components/product/up-load-image.vue'
export default {
  name: 'RecommendDialog',
  components: {
    UpLoadImage
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    const validateType = (rule, value, callback) => {
      if (this.formValidate.type) {
        if (this.formValidate.type === 'THIRD_PARTY_LINK') {
          if (this.formValidate.recommendUrl) {
            callback()
          } else {
            callback(new Error(this.$t('appMarketing.recommend.prompt.recommendUrl')))
          }
        } else {
          callback()
        }
      } else {
        callback(new Error(this.$t('appMarketing.recommend.table.type') + this.$t('common.null')))
      }
    }
    const validateFile = (rule, value, callback) => {
      if (this.formValidate.id) {
        if (this.formValidate.url || this.file) {
          callback()
        } else {
          callback(new Error(this.$t('appMarketing.recommend.table.img') + this.$t('common.null')))
        }
      } else {
        if (this.file) {
          callback()
        } else {
          callback(new Error(this.$t('appMarketing.recommend.table.img') + this.$t('common.null')))
        }
      }
    }
    return {
      submitLoading: false,
      file: null,
      formValidate: {
        id: null,
        name: null,
        sort: null,
        type: null,
        recommendUrl: null,
        state: null,
        url: null
      },
      rules: {
        name: [{ required: true, message: this.$t('appMarketing.recommend.table.name') + this.$t('common.null'), trigger: 'blur' }],
        sort: [{ required: true, message: this.$t('appMarketing.recommend.table.recommendSort') + this.$t('common.null'), trigger: 'blur' }],
        type: [{ required: true, validator: validateType, trigger: 'blur' }],
        file: [{ required: true, validator: validateFile, trigger: 'blur' }],
        state: [{ required: true, message: this.$t('appMarketing.recommend.table.name') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    getImgUrl(file) {
      console.log(file)
      this.file = file
    },
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs.upload.init()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = new FormData()
          data.append('id', this.formValidate.id)
          data.append('name', this.formValidate.name)
          data.append('sort', this.formValidate.sort)
          data.append('type', this.formValidate.type)
          data.append('state', this.formValidate.state)
          if (this.formValidate.type === 'THIRD_PARTY_LINK') {
            data.append('recommendUrl', this.formValidate.recommendUrl)
          }
          if (this.file) {
            data.append('file', this.file)
          } else {
            if (this.formValidate.url) {
              data.append('url', this.formValidate.url)
            }
          }
          if (this.formValidate.id) {
            updateRecommend(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addRecommend(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        }
      })
    },
    changeChecked(val) {
      this.checked = val
    },
    changeRadio(val) {
      console.log(val)
      this.radio = val.toString()
    },
    changeRadioValue(data, val) {
      this[data] = val
    },
    setTimeValue(start, end) {
      this.time = [start, end]
    }
  }
}
</script>

<style scoped>

</style>
